﻿<MudPaper Class="pa-4">
    <MudChart ChartType="ChartType.Pie" ChartSeries="@_data.AsChartDataSet()" @bind-SelectedIndex="_index" ChartLabels="@_labels" Width="300px" Height="300px" />
</MudPaper>
<MudPaper Class="pa-4 mt-2 d-flex justify-center">
    <MudButton OnClick="AddDataSize" Variant="Variant.Filled" Color="Color.Primary">Add</MudButton>
    <MudButton @onclick="RandomizeData" Variant="Variant.Filled" Class="mx-4">Randomize</MudButton>
    <MudButton OnClick="RemoveDataSize" Variant="Variant.Filled" Color="Color.Secondary">Remove</MudButton>  
</MudPaper>
<MudText Typo="Typo.h6">Selected portion of the chart: @_index</MudText>

@code {
    private int _index = -1; //default value cannot be 0 -> first selectedindex is 0.
    private int _dataSize = 4;
    private double[] _data = [77, 25, 20, 5];

    private readonly string[] _labels =
    [
        "Uranium", "Plutonium", "Thorium", "Caesium", "Technetium", "Promethium",
        "Polonium", "Astatine", "Radon", "Francium", "Radium", "Actinium", "Protactinium",
        "Neptunium", "Americium", "Curium", "Berkelium", "Californium", "Einsteinium", "Mudblaznium"
    ];


    private void RandomizeData()
    {
        var newData = new double[_dataSize];
        for (int i = 0; i < newData.Length; i++)
            newData[i] = Math.Round(Random.Shared.NextDouble() * 100, 2);
        _data = newData;
        StateHasChanged();
    }

    private void AddDataSize()
    {
        if (_dataSize < 20)
        {
            _dataSize += 1;
            RandomizeData();
        }
    }

    private  void RemoveDataSize()
    {
        if (_dataSize > 0)
        {
            _dataSize -= 1;
            RandomizeData();
        }
    }
}
